import mysql from "mysql";

const mysqlConfig = require ('../config/mysql.config');

export const connection = mysql.createConnection(mysqlConfig);

const pool = mysql.createPool(mysqlConfig)

export let query = function( sql:any, values:any ) {
    // 返回一个 Promise
    return new Promise(( resolve, reject ) => {
      pool.getConnection(function(err, connection) {
        if (err) {
          reject( err )
        } else {
          connection.query(sql, values, ( err, rows) => {
  
            if ( err ) {
              reject( err )
            } else {
              resolve( rows )
            }
            // 结束会话
            connection.release()
          })
        }
      })
    })
  }
  
//   module.exports =  query